package javaBase.chapter02;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;

import java.util.Scanner;
import java.util.Random;

public class LuckyAudience {
    private String[] audienceList;
    private int audienceCount;
    private Random random;

    public LuckyAudience() {
        audienceList = new String[3];
        audienceCount = 0;
        random = new Random();
    }

    public void addAudience(String name) {
        if (audienceCount < 3) {
            audienceList[audienceCount] = name;
            audienceCount++;
            System.out.println("观众 " + name + " 添加成功！");
        } else {
            System.out.println("观众已满，无法添加！");
        }
    }

    public void displayAudience() {
        System.out.println("当前观众列表：");
        for (int i = 0; i < audienceCount; i++) {
            System.out.println((i + 1) + ". " + audienceList[i]);
        }
    }

    public void pickLuckyAudience() {
        if (audienceCount > 0) {
            int luckyIndex = random.nextInt(audienceCount);
            String luckyAudience = audienceList[luckyIndex];
            System.out.println("幸运观众是：" + luckyAudience);
        } else {
            System.out.println("当前没有观众！");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LuckyAudience luckyAudience = new LuckyAudience();

        int choice;
        do {
            System.out.println("\n====== 幸运观众抽取程序 ======");
            System.out.println("1. 添加观众");
            System.out.println("2. 总览观众");
            System.out.println("3. 抽取幸运观众");
            System.out.println("0. 退出");
            System.out.print("请输入您的选择：");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("请输入观众姓名：");
                    scanner.nextLine(); // Consume newline left-over
                    String name = scanner.nextLine();
                    luckyAudience.addAudience(name);
                    break;
                case 2:
                    luckyAudience.displayAudience();
                    break;
                case 3:
                    luckyAudience.pickLuckyAudience();
                    break;
                case 0:
                    System.out.println("程序已退出。");
                    break;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        } while (choice != 0);

        scanner.close();
    }
}
